import Vue from "vue";
import VueRouter from "vue-router";
import $api from '@/http/api.js'
import store from '@/store';

Vue.use(VueRouter);
import Login from "../views/login/Login.vue";
import Register from "../views/register/Register.vue";
import Index from "../views/index/Index.vue";
import AdminList from "../components/adminList/AdminList.vue";
import SwiperList from "../components/adminList/SwiperList.vue";
import SwiperAdd from "../components/adminList/SwiperAdd.vue";
import MenuList from "../components/adminList/MenuList.vue";
import VipList from "../components/adminList/VipList.vue";
import ProductList from "../components/adminList/ProductList.vue";
import Chart from "../components/adminList/Chart.vue";
//一般不常进入的页面会使用懒加载
const Error = () => import("../views/404/Error.vue");



const routes = [
  {
    path: "/",
    redirect: "/login" //重定向
  },
  {
    path: "/login",
    name: "Login",
    component: Login
  },
  {
    path: "/register",
    name: "Register",
    component: Register
  },
  {
    path: "/index",
    redirect: "/index/chart",
    name: "Index",
    component: Index,
    // beforeEnter: (to, from, next) => {
    //   const token = localStorage.token;
    //   if (token) {
    //     // 判断 token 是否过期
    //     // 发送“获取用户信息”的请求，将 token 携带到后端验证有效期
    //     $api.userApi.getUserInfo().then(({ data }) => {
    //       if (data.code) {
    //         store.commit('GetUserInfo', data.data);
    //         next();
    //       }
    //     })
    //   } else {
    //     store._vm.$message.error('你还未登录，请先登录')
    //     next('/login');
    //   }
    // },
    children: [
      {
        path: "chart",
        name: "chart",
        component: Chart,
        meta: {
          iskeepalive: false,
          title: '图表'
        }
      },
      {
        path: "adminList",
        name: "adminList",
        component: AdminList,
        meta: {
          iskeepalive: false,
          title: '管理员列表'
        }
      },
      {
        path: "swiperList",
        name: "swiperList",
        component: SwiperList,
        meta: {
          iskeepalive: false,
          title: '轮播图列表'
        }
      },
      {
        path: "swiperAdd",
        name: "swiperAdd",
        component: SwiperAdd,
        meta: {
          iskeepalive: false,
          title: '新增轮播图'
        }
      },
      {
        path: "menuList",
        name: "menuList",
        component: MenuList,
        meta: {
          iskeepalive: false,
          title: '菜谱列表'
        }
      },
      {
        path: "vipList",
        name: "vipList",
        component: VipList,
        meta: {
          iskeepalive: false,
          title: '会员列表'
        }
      },
      {
        path: "productList",
        name: "productList",
        component: ProductList,
        meta: {
          iskeepalive: false,
          title: '套餐列表'
        }
      },

    ]
  },
  {
    path: "*",
    name: "Error",
    component: Error
  }
];



const router = new VueRouter({
  routes: routes, //路由配置
  // mode: 'history'

});


//加载进度条
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
// 配置
NProgress.configure({
  easing: 'ease', // 动画方式
  speed: 600, // 递增进度条的速度
  showSpinner: true, // 是否显示加载ico
  trickleSpeed: 200, // 自动递增间隔
  minimum: 0.3 // 初始化时的最小百分比
})

router.beforeEach((to, from, next) => { // 每次切换页面时，调用进度条
  NProgress.start(); // 这个一定要加，没有next()页面不会跳转的。这部分还不清楚的去翻一下官网就明白了
  next();
});
router.afterEach(() => { // 在即将进入新的页面组件前，关闭掉进度条
  NProgress.done()
})


export default router;
